export type SortType =
  | 'lastName:ASC'
  | 'lastName:DESC'
  | 'firstName:ASC'
  | 'firstName:DESC'

export enum SortTypes {
  ln_asc = 'lastName:ASC',
  ln_desc = 'lastName:DESC',
  fn_asc = 'firstName:ASC',
  fn_desc = 'firstName:DESC'
}
interface IState {
  // select options
  orgs: { orgRu: string; orgEn: string }[]
  positions: {
    positionRu: string
    positionEn: string | null
    positionId: string
  }[]
  cities: { cityRu: string; cityEn: string | null }[]
  sort: {
    sortNameRu: string
    sortNameEn: string | null
    sortType: SortType | ''
  }[]
  // input by user
  filter: {
    selectedCities: string[]
    selectedOrgs: string[]
    selectedPositions: string[]
    selectedSort: SortType | ''
  }
}

const initState: IState = {
  orgs: [],
  positions: [],
  cities: [],
  sort: [
    {
      sortNameRu: 'Нет',
      sortNameEn: null,
      sortType: ''
    },
    {
      sortNameRu: 'Фамилии, от A до Я',
      sortNameEn: null,
      sortType: SortTypes.ln_asc
    },
    {
      sortNameRu: 'Фамилии, от Я до А',
      sortNameEn: null,
      sortType: SortTypes.ln_desc
    },
    {
      sortNameRu: 'Имени, от А до Я',
      sortNameEn: null,
      sortType: SortTypes.fn_asc
    },
    {
      sortNameRu: 'Имени, от Я до А',
      sortNameEn: null,
      sortType: SortTypes.fn_desc
    }
  ],
  filter: {
    selectedSort: '',
    selectedCities: [],
    selectedOrgs: [],
    selectedPositions: []
  }
}

export const useFilterStore = defineStore('filterStore', {
  state: () => ({
    ...initState
  }),
  getters: {
    getCityOptions: (state) => {
      return state.cities.map((el) => ({
        label: el.cityRu,
        value: el.cityRu
      }))
    },
    getPositionOptions: (state) => {
      return state.positions.map((el) => ({
        label: el.positionRu,
        value: el.positionRu
      }))
    },
    getOrgsOptions: (state) => {
      return state.orgs.map((el) => ({
        label: el.orgRu,
        value: el.orgRu
      }))
    },
    getSortOptions: (state) => {
      return state.sort.map((el) => ({
        label: el.sortNameRu,
        value: el.sortType
      }))
    }
  }
})
